package springapp.util;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import springapp.entity.ICacheEntity;
import springapp.jaxb.IConfig;
import springapp.jaxb.IElement;

import com.google.gson.Gson;

public class Utils {
	
	public static boolean isEqual(Object object2, Object object1) {
		if (object2 == object1) {
			return true;
		}
		if (object1 != null && object1.equals(object2)) {
			return true;
		}
		return false;
	}

	public static float getWeightedAverage(Float rating, int num, int rating2) {
		return (rating*num + (float)rating2)/(num+1);
	}
	
	public static String getCommaSeperatedList(Collection<?> values){
		StringBuilder builder = new StringBuilder();
		for (Object object : values) {
			builder.append(object);
			builder.append(",");
		}
		builder.replace(builder.length()-1, builder.length(), "");
		return builder.toString();
	}
	
	public static String getCommaSeperatedListForCacheEntity(Set<ICacheEntity> entities){
		StringBuilder builder = new StringBuilder();
		for (ICacheEntity entity : entities) {
			builder.append(entity.getName());
			builder.append(",");
		}
		builder.replace(builder.length()-1, builder.length(), "");
		return builder.toString();
	}
	
	public static String getCommaSeperatedListForQuery(String[] columns,String tableAlias){
		StringBuilder builder = new StringBuilder();
		for (String column : columns) {
			builder.append(tableAlias).append(".").append(column);
			builder.append(",");
		}
		builder.replace(builder.length()-1, builder.length(), "");
		return builder.toString();
	}
	
	public static int getInt(String val){
		return Integer.parseInt(val.trim());
	}

	public static Float getFloat(String parameter) {
		return Float.parseFloat(parameter.trim());
	}
	
	public static String getJson(Object object){
		Gson gson = new Gson();
		String json = gson.toJson(object);
		return json;
	}
	
	public static <E extends ICacheEntity> Set<String> getNamesFromCacheEntity(Set<E> entities){
		Set<String> names = new HashSet<String>();
		for (ICacheEntity cacheEntity : entities) {
			names.add(cacheEntity.getName());
		}
		return names;
	}
	
	@SuppressWarnings("unchecked")
	/**
	 * reads Jaxb config using file name and param config
	 * @param filename
	 * @param config
	 * @return
	 * @throws JAXBException
	 */
	public static <T extends IConfig> T getConfig(InputStream stream, T config)
			throws JAXBException {
		JAXBContext context = JAXBContext.newInstance(config.getClass());
		Unmarshaller unmarshaller = context.createUnmarshaller();
		config = (T) unmarshaller.unmarshal(stream);
		return config;
	}
	
	/**
	 * returns nameVsElementMap for list
	 * @param list
	 * @return
	 */
	public static <E extends IElement> Map<String, E> getNameMap(List<E> list) {
		Map<String, E> names = new HashMap<String, E>();
		for (E e : list) {
			names.put(e.getName(), e);
		}
		return names;
	}
	
	/**
	 * gets the differential of the two lists (allElements - elements)
	 * @param allElements
	 * @param elements
	 */
	public static <K extends IElement, V extends ICacheEntity> void removeOldElements(
			List<K> allElements, List<V> elements) {
		Map<String, K> namesMap = getNameMap(allElements);

		for (V entity : elements) {
			K element = namesMap.get(entity.getName());
			if (element != null) {
				allElements.remove(element);
			}
		}
	}

	
}
